package com.module.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.framework.constants.SystemConstants;
import com.module.framework.domain.*;
import com.module.framework.dto.UserPwdDTO;
import com.module.framework.mapper.NowLendMapper;
import com.module.framework.mapper.UserMapper;
import com.module.framework.service.RoleService;
import com.module.framework.service.UserService;
import com.module.framework.utils.BeanCopyUtils;
import com.module.framework.vo.PageVO;
import com.module.framework.vo.ProductDetailVO;
import com.module.framework.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author JiHuiQin
 * @Date 2022/3/2 20:53
 * @Version 1.0
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private NowLendMapper nowLendMapper;

    @Override
    public PageVO getUserList(Integer pageNum, Integer pageSize, String name, Integer roleId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(name), User::getUserName, name);
        queryWrapper.eq(Objects.nonNull(roleId) && roleId > 0, User::getRoleId, roleId);
        pageSize = pageSize == null ? SystemConstants.PAGE_SIZE : pageSize;
        Page<User> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        List<User> records = page.getRecords();
        for (User record : records) {
            Role role = roleService.getById(record.getRoleId());
            record.setRoleName(role.getName());
        }
        PageVO pageVO = new PageVO(records, (int) page.getTotal());
        return pageVO;
    }

    @Override
    public PageVO getUserList2() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(User::getRoleId, 3);
        List<User> lists = list(queryWrapper);
        PageVO pageVO = new PageVO(lists, lists.size());
        return pageVO;
    }

    @Override
    public UserInfoVO getUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        UserInfoVO userInfoVO = BeanCopyUtils.copyBean(loginUser.getUser(), UserInfoVO.class);
        return userInfoVO;
    }

    @Override
    public void addUser(User user) {
        user.setBan(0);
        user.setLendNumber(0);
        String password = user.getPassword();
        System.out.println("password:" + password);
        BCryptPasswordEncoder pa = new BCryptPasswordEncoder();
        String encode = pa.encode(password);
        user.setPassword(encode);
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void updateBan(Long uid, Integer id) {
        NowLend nowLend = nowLendMapper.selectById(id);
        nowLend.setDeadFlag(1);
        nowLendMapper.updateById(nowLend);

        User user = getById(uid);
        Integer ban = user.getBan() + 1;
        user.setBan(ban);
        userMapper.updateById(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void updatePwd(UserPwdDTO userPwdDTO) {
        // 密码加密
        String password = userPwdDTO.getPassword();
        BCryptPasswordEncoder pa = new BCryptPasswordEncoder();
        String encode = pa.encode(password);
        userPwdDTO.setPassword(encode);
        User user = BeanCopyUtils.copyBean(userPwdDTO, User.class);
        userMapper.updateById(user);
    }

}
